import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../pages/home/home.vue";
import Cart from "../pages/car/car.vue";
import My from "../pages/my/my.vue";
import Type from "../pages/type/type.vue";
import List from "../pages/list/list.vue";
import Detail from "../pages/detail/detail.vue";
import Login from "../pages/login/login.vue";
import Pay from "../pages/pay/pay.vue";
import Order from "../pages/order/order.vue";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      component: Home,
    },
    {
      path: "/pay",
      name: "pay",
      component: Pay,
    },
    {
      path: "/type",
      name: "type",
      component: Type,
    },
    {
      path: "/list/:id?",
      name: "list",
      component: List,
      props: true,
    },
    {
      path: "/detail/:id?",
      name: "detail",
      component: Detail,
      props: true,
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/cart",
      name: "cart",
      component: Cart,
    },
    {
      path: "/my",
      name: "my",
      component: My,
    },
    {
      path: "/order:status?",
      name: "order",
      component: Order,
      props: true,
    },
  ],
});
const whiteRouter = ["my", "cart"]; //需要权限的；=路由
router.beforeEach(function (to, from, next) {
  if (whiteRouter.includes(to.name) && !localStorage.getItem("token")) {
    //token是没有登录让其跳转到登录页面
    return next("/login");
  } else {
    next();
  }
});
export default router;
